package com.mall.b2c.template.directive;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.mall.b2c.bean.GoodsCategoryBean;
import com.mall.b2c.hessian.CacheService;
import com.mall.common.directive.DirectiveUtils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 获取指定条件的分类列表
 */
@Repository
public class GoodsCategoryListDirective extends BaseDirective {

	/** 输入参数，分类IDS**/
	public static final String PARAM_GOODS_CATEGORY_IDS = "cateIds";
	/**是否显示**/
	public static final String PARAM_ISVISIBLE = "isVisible";
	/** 输入参数，父分类ID*/
	public static final String PARAM_PARENT_ID = "parentId";
	/** 是否是一级目录*/
	public static final String PARAM_ISTOP = "isTop";
	/**是否推荐*/
	public static final String PARAM_ISRECOMMED = "isRecommend";
	/** 限制显示数量 **/
	public static final String PARAM_COUNT = "count";
	
	@Resource private CacheService cacheService;
	
	@SuppressWarnings({"rawtypes", "unchecked"})
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		Long[] goodsCategoryIds = DirectiveUtils.getLongArray(PARAM_GOODS_CATEGORY_IDS, params);
		Long parentId = DirectiveUtils.getLong(PARAM_PARENT_ID, params);
		Integer count = DirectiveUtils.getInt(PARAM_COUNT, params);
		Boolean isRecommend = DirectiveUtils.getBool(PARAM_ISRECOMMED, params);
		Boolean isVisible = DirectiveUtils.getBool(PARAM_ISVISIBLE, params);
		
		List<GoodsCategoryBean> list = cacheService.getList(
				goodsCategoryIds, parentId, isRecommend, isVisible, count);
		renderParamToBody("goodsCategories", list, env, body);
	}
	
}
